package org.dallasrowing.logbook.ui.view;

import org.dallasrowing.model.Boat;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

public class SwtBoatEditorWidget extends SwtBeanEditorWidgetBase<Boat> {

	private Button retired;
	private Text seats;
	private Button withCox;
	private Text name;


	public SwtBoatEditorWidget(Composite parent) {
		super(parent, SWT.NONE);
	}

	@Override
	protected void createBeanFields() {
		Group g = new Group(this, SWT.BORDER);
		g.setLayout(new GridLayout(2, false));
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.horizontalSpan = 2;
		g.setLayoutData(gd);
		
		gd = new GridData(GridData.FILL_HORIZONTAL);
		new Label(g, SWT.NONE).setText("Name:");
		name = new Text(g, SWT.BORDER);
		name.setLayoutData(gd);
		
		gd = new GridData(GridData.FILL_HORIZONTAL);
		new Label(g, SWT.NONE).setText("Cox Seat:");
		withCox = new Button(g, SWT.CHECK);
		withCox.setEnabled(false);
		withCox.setLayoutData(gd);
		
		gd = new GridData(GridData.FILL_HORIZONTAL);
		new Label(g, SWT.NONE).setText("Seats:");
		seats = new Text(g, SWT.BORDER);
		seats.setLayoutData(gd);
		seats.setEnabled(false);
		
		gd = new GridData(GridData.FILL_HORIZONTAL);
		new Label(g, SWT.NONE).setText("Is Retired:");
		retired = new Button(g, SWT.CHECK);		
		retired.setLayoutData(gd);
	}

	@Override
	protected void disableEdit() {
		name.setEnabled(false);
		retired.setEnabled(false);
		seats.setEnabled(false);
		withCox.setEnabled(false);
	}

	@Override
	protected void enableEdit() {
		name.setEnabled(true);
		name.setText("");
		retired.setEnabled(true);
		retired.setSelection(false);
		seats.setEnabled(true);
		seats.setText("");
		withCox.setEnabled(true);
		withCox.setSelection(false);
	}

	@Override
	protected String getBeanName(Boat boat) {
		return boat.getName();
	}

	public void updateBean(Boat boat) {
		boat.setName(name.getText());
		boat.setRetired(retired.getSelection());
		boat.setSeats(Integer.parseInt(seats.getText()));
		boat.setWithCox(withCox.getSelection());
	}

	public void setBean(Boat boat) {
		name.setText(boat.getName());
		retired.setSelection(boat.isRetired());
		withCox.setSelection(boat.isWithCox());
		seats.setText(""+boat.getSeats());
		//enabling these fields could compromise the integrity of the database
		seats.setEnabled(false);
		withCox.setEnabled(false);
	}

}
